//
//  ButtomView.m
//  TaTaBang
//
//  Created by 李禹 on 15/8/16.
//  Copyright (c) 2015年 hailong. All rights reserved.
//

#import "ButtomView.h"

@interface ButtomView ()

@property (nonatomic, weak) UIButton *leftBtn;
@property (nonatomic, weak) UIButton *rightBtn;
@property (nonatomic, weak) UIView *lineView;

@end

@implementation ButtomView

+ (instancetype)buttomViewWithLeftTitle:(NSString *)leftTitle rightTitle:(NSString *)rightTitle {
    return [[self alloc] initWithLeftTitle:leftTitle rightTitle:rightTitle];
}

- (instancetype)initWithLeftTitle:(NSString *)leftTitle rightTitle:(NSString *)rightTitle {
    if (self = [super init]) {
        UIButton *leftBtn = [[UIButton alloc] init];
        [leftBtn setTitle:leftTitle forState:UIControlStateNormal];
        [leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        leftBtn.backgroundColor = [UIColor lightGrayColor];
        leftBtn.tag = ButtomViewTypeLeft;
        [leftBtn addTarget:self action:@selector(buttomButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:leftBtn];
        self.leftBtn = leftBtn;
        
        UIButton *rightBtn = [[UIButton alloc] init];
        [rightBtn setTitle:rightTitle forState:UIControlStateNormal];
        [leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        rightBtn.backgroundColor = [UIColor redColor];
        rightBtn.tag = ButtomViewTypeRight;
        [rightBtn addTarget:self action:@selector(buttomButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:rightBtn];
        self.rightBtn = rightBtn;
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.leftBtn.width = self.rightBtn.width = self.width / 2;
    self.leftBtn.height = self.rightBtn.height = self.height;
    self.leftBtn.x = 0;
    self.leftBtn.y = self.rightBtn.y = 0;
    self.rightBtn.x = self.width / 2;
}

- (void)buttomButtonDidClick:(UIButton *)btn {
    if ([self.delegate respondsToSelector:@selector(buttomView:didSelectedButtomViewType:)]) {
        [self.delegate buttomView:self didSelectedButtomViewType:(ButtomViewType)btn.tag];
    }
}

- (void)setRightTitle:(NSString *)rightTitle {
    _rightTitle = rightTitle;
    [self.rightBtn setTitle:rightTitle forState:UIControlStateNormal];
}
@end
